/**
 * Created by zyy on 15/5/5.
 * zhangyuyu@superjia.com
 */
(function ($) {
    var action = {

        __container: null,
        __options: null,

        __filter: '',
        __path: '',

        __action: '',

        __init: false,

        init: function (container, options) {
            var self = this
            self.__container = container
            self.__options = options

            self.__filter = options.filter
            self.__path = options.path

            var fileItems = options.fileItems

            var action = self.__action = options.action

            if (!self.__init) {
                self.__init = true
                self.__panel = $(tpl).appendTo(container)
            }

            self.__panel.empty()

            var utils = App.common.modules.util

            switch (action) {
                case 'newfolder':
                    self.__initNewFolder()
                    self.__show()
                    break
                default:
                    if (!fileItems || !fileItems.length) {
                        App.common.modules.smallnote('请选择一个文件', {
                            pattern: 'error'
                        })
                        return false
                    } else {
                        if (action == 'link' && fileItems.length > 1) {
                            App.common.modules.smallnote('只能分享一个文件，你选择了' + fileItems.length + '个', {
                                pattern: 'error'
                            })
                            return false
                        }
                        if (action == 'shareto') {
                            if (fileItems.length > 1 || !fileItems[0].get('dir')) {
                                App.common.modules.smallnote('只能选择一个文件夹', {
                                    pattern: 'error'
                                })
                                return false
                            }
                        }
                        App.modules[action + 'Action'].init(self.__panel, options)
                        self.__show()
                    }
                    break
            }
        },

        __show: function () {
            var self = this
            self.__panel.addClass('active')
        },

        hide: function (needFileList) {
            var self = this
            self.__panel.removeClass('active')

            needFileList && $.jps.publish('file-list', {
                filter: self.__filter,
                path: self.__path
            })
        },

        __initNewFolder: function () {
            var self = this
            var panel = self.__panel

            panel.html(newfolderTpl).find('.folder-input').focus()

            self.__bindNewFolderEvent()
        },

        __bindNewFolderEvent: function () {
            var self = this
            var panel = self.__panel
            panel.find('.back').click(function () {
                if($.trim(panel.find('.folder-input').val())) {
                    if(confirm('是否放弃新建此文件夹')){
                        self.hide()
                    }
                    /*new App.common.modules.Dialog({
                        showTitle: false,
                        width: 300,
                        message: '<div style="padding: 40px 20px;background: #fff;">是否放弃新建此文件夹</div>',
                        isConfirm: true,
                        limitHeight: false,
                        fullScreen: true,
                        okCallback: function() {
                            self.hide()
                        }
                    })*/
                } else {
                    self.hide()
                }
            })

            panel.find('.opt-btn').click(function () {
                var item = $(this)
                if(item.hasClass('disabled')) return false
                var folderInput = panel.find('.folder-input')
                var name = $.trim(folderInput.val())
                if (!name) {
                    App.common.modules.smallnote('请输入文件夹名称', {
                        pattern: 'error'
                    })
                    folderInput.focus()
                    return false
                }
                item.addClass('disabled')
                $.ajax({
                    url: '/review/file/webmkdir/',
                    method: 'post',
                    dataType: 'json',
                    data: {
                        newDir: self.__path + name + '/'
                    },
                    success: function (data) {
                        self.hide()
                        $.jps.publish('file-list', {
                            filter: self.__filter,
                            path: self.__path
                        })
                    }
                })

            })
        }
    }

    var tpl = '<div class="mod-action"></div>'

    var newfolderTpl = '' +
        '<div class="mod-newfolder">' +
        '   <div class="action-header">' +
        '       <a class="back">&lt;</a><div class="title">新建文件夹</div><div class="opt-btn">保存</div>' +
        '   </div>' +
        '   <div class="input-wrap">' +
        '       <em class="ficon-folder"></em>' +
        '       <input class="folder-input" placeholder="文件夹名称" />' +
        '   </div>' +
        '   <div class="tip">请命名您的新文件夹</div>' +
        '</div>'

    App.modules.action = action
})(jQuery)